package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetCopyrightEvent;
	import net.optix.ip.events.GetInventionEvent;
	import net.optix.ip.events.GetTradeMarkEvent;
	import net.optix.ip.events.ReturnCaseEvent;
	import net.optix.ip.model.ModelLocator;
	
	public class ReturnCaseCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();

		private var type:int;
		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:ReturnCaseEvent = ReturnCaseEvent( event );
			this.type	=	myEvent.category;
			var delegate:BudgetDelegate = new BudgetDelegate(  );     
			delegate.returnCase( this , myEvent.id,myEvent.category );
			
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			trace(event.result);
			var title:String	=	"Result of return case";
			var message:String;

			var	result:int;		
			
			try
			{
				result	=	event.result.result;
			}
			catch(error:Error)
			{
				result	=	0;
			}
			
			if(result == 1)
			{
				message	=	"Successfully return !";
				refresh();
			}
			else if(result == -2)
			{
				message	=	"Access Denied !";
			}
			else
			{
				message	=	"Program error !";
			}
			
			model.bSubmit	=	true;
			
			var alert:Alert	=	 
				Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
			alert.width		=	360;
			alert.height	=	130;
		}
		
		private function refresh():void
		{
			
			//var getBudgets:GetBudgetsEvent	;
			switch(this.type)
			{
				case 0:
					var getInvention:GetInventionEvent	=	new GetInventionEvent(model.currentInvention.inventionID);
					com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getInvention);
					//getBudgets	=	new GetBudgetsEvent(model.currentInvention.inventionID,0);
					break;
				case 1:
					var getTradeMark:GetTradeMarkEvent	=	new GetTradeMarkEvent(model.currentTradeMark.tradeMarkID,
						model.userID,model.role);
					CairngormEventDispatcher.getInstance().dispatchEvent(getTradeMark);
					//getBudgets	=	new GetBudgetsEvent(model.currentTradeMark.tradeMarkID,1);
					break;
				case 2:
					var getCopyright:GetCopyrightEvent	=	new GetCopyrightEvent(model.currentCopyright.copyrightID,model.userID,model.role);
					CairngormEventDispatcher.getInstance().dispatchEvent(getCopyright);
					//getBudgets	=	new GetBudgetsEvent(model.currentCopyright.copyrightID,2);
					break;
			}
			//CairngormEventDispatcher.getInstance().dispatchEvent(getBudgets);
		}
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			
		}

	}
}